package com.coocare.licence.config.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.coocare.licence.entity.SysMenu;
import com.coocare.licence.entity.SysUserRole;
import com.coocare.licence.service.ISysMenuService;
import com.coocare.licence.service.ISysUserRoleService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@RequiredArgsConstructor
public class StpInterfaceImpl implements StpInterface {

    private final ISysUserRoleService sysUserRoleService;
    private final ISysMenuService menuService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<SysUserRole> userRoles = sysUserRoleService.list(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, loginId));
        return menuService.listByIds(userRoles.stream().map(SysUserRole::getRoleId).toList()).stream().map(SysMenu::getPermission).toList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<SysUserRole> userRoles = sysUserRoleService.list(Wrappers.lambdaQuery(SysUserRole.class).eq(SysUserRole::getUserId, loginId));
        return userRoles.stream().map(userRole -> userRole.getRoleId()+"").toList();
    }
}
